package com.qianguo.buyer.address;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;
import com.qianguo.buyer.R;
import com.qianguo.buyer.base.BaseTitleActivity;
import com.qianguo.buyer.entity.BuyerAddress;
import com.qianguo.buyer.entity.BuyerAddressList;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


/**
 * 收货地址
 * @author eva
 * @date 16/8/18
 */
@EActivity(R.layout.activity_address_list)
public class BuyerAddressActivity extends BaseTitleActivity {

    @ViewById(R.id.buyer_address_recycler_view)
    SwipeRefreshRecyclerView mAddressRecyclerView;

    @ViewById(R.id.buyer_address_add_text_view)
    TextView mTvAdd;

    private AddressAdapter mAdapter;
    private List<BuyerAddress> mAddressList = new ArrayList<>();

    public static void jumpToChooseAddress(Context context) {
        context.startActivity(new Intent(context, BuyerAddressActivity_.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AddAddressEvent event) {
        getAddressList();
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.buyer_address_choose);
    }

    @Override
    protected void initializeUI() {
        EventBus.getDefault().register(this);
        mAdapter = new AddressAdapter(this);
        mAddressRecyclerView.setIsLoadMore(false);
        mAddressRecyclerView.setRefreshable(false);
        mAddressRecyclerView.setAdapter(mAdapter);
        mAddressRecyclerView.setOnItemClickListener(new SwipeRefreshRecyclerView.OnRecyclerItemClickListener() {
            @Override
            public void onClick(int position) {
                EventBus.getDefault().post(mAddressList.get(position));
                finish();
            }
        });
        getAddressList();
    }

    @Click(R.id.buyer_address_add_text_view)
    void clickAddAddress() {
        BuyerAddAddressActivity.jumpToAddAddress(this);
    }

    @Background
    void getAddressList() {
        mApp.getApi().getAddressList(mApp.getUser().getToken(), new HttpRequestCallback<BuyerAddressList>() {
            @Override
            public void onSuccess(int code, String msg, BuyerAddressList data) {
                mAddressList.clear();
                mAddressList.addAll(data.getList());
                refreshUI();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(BuyerAddressActivity.this, msg);
            }
        });
    }

    @UiThread
    void refreshUI() {
        mAddressRecyclerView.notifyDataSetChanged();
    }

    private class AddressAdapter extends RecyclerView.Adapter<AddressViewHolder> {

        private Context context;

        public AddressAdapter(Context context) {
            this.context = context;
        }

        @Override
        public AddressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(context).inflate(R.layout.item_address, null);
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
                    RecyclerView.LayoutParams.MATCH_PARENT,
                    RecyclerView.LayoutParams.WRAP_CONTENT);
            v.setLayoutParams(lp);
            return new AddressViewHolder(v);
        }

        @Override
        public void onBindViewHolder(AddressViewHolder holder, int position) {
            holder.onBind(mAddressList.get(position));
        }

        @Override
        public int getItemCount() {
            return mAddressList == null ? 0 : mAddressList.size();
        }
    }

    private class AddressViewHolder extends RecyclerView.ViewHolder {
        TextView mTvReceiver;
        TextView mTvMobile;
        TextView mTvAddressType;
        TextView mTvAddress;
        TextView mTvEdit;

        public AddressViewHolder(View itemView) {
            super(itemView);
            mTvReceiver = (TextView) itemView.findViewById(R.id.item_address_receiver_text_view);
            mTvMobile = (TextView) itemView.findViewById(R.id.item_address_mobile_text_view);
            mTvAddressType = (TextView) itemView.findViewById(R.id.item_address_type_text_view);
            mTvAddress = (TextView) itemView.findViewById(R.id.item_address_text_view);
            mTvEdit = (TextView) itemView.findViewById(R.id.item_address_edit_text_view);
        }

        public void onBind(BuyerAddress address) {
            mTvReceiver.setText(address.getReceiver());
            mTvMobile.setText(address.getMobile());
            mTvAddressType.setText("[" + BuyerAddress.AddressType.get(address.getAddType()).toString() + "]");
            mTvAddress.setText(address.getAddress());
            mTvEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 16/8/18 edit
                }
            });

        }
    }
}
